package com.xbongbong.paas.handle;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.pojo.dto.FormDataListDTO;
import com.xbongbong.paas.pojo.vo.PaasFormDataEsListVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.product.pojo.vo.RelativeProductExplainVO;
import com.xbongbong.pro.weblist.pojo.ListOptionPackagePojo;
import com.xbongbong.saas.enums.ListOptionEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.ClueEnum;
import com.xbongbong.saas.enums.business.ContractEnum;
import com.xbongbong.saas.enums.business.CustomerManagementEnum;
import com.xbongbong.saas.enums.business.OrderEnum;
import com.xbongbong.saas.help.SaasListHelp;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author 吴峰
 * @version v1.0
 * @date 2020/3/24 16:21
 * @since v1.0
 */
@Component
public abstract class AbstractOptionHandle {

    @Resource
    private SaasListHelp saasListHelp;

    /**
     * 在解释里设置操作方式
     * @author 吴峰
     * @date 2020/3/24 16:36
     * @param formDataListDTO
     * @param explainList
     * @param paasFormDataEsListVO
     * @return void
     * @throws XbbException 业务异常
     */
    public abstract void setOptionsInExplain(FormDataListDTO formDataListDTO, List<FieldAttrEntity> explainList, PaasFormDataEsListVO paasFormDataEsListVO) throws XbbException;

    /**
     * 在数据里设置操作方式
     * @author 吴峰
     * @date 2020/3/24 16:36
     * @param formDataListDTO
     * @param paasFormDataEsListVO 数据列表
     * @return void
     * @throws XbbException 业务异常
     */
    public abstract void setOptionsInData(FormDataListDTO formDataListDTO, PaasFormDataEsListVO paasFormDataEsListVO) throws XbbException;

    /**
     * 在第一个接口内，所要加的按钮数据和标签数据
     * @param formDataListDTO
     * @param paasFormDataEsListVO
     * @return
     * @throws XbbException
     */
    public abstract ListOptionPackagePojo getFirstOptionData(FormDataListDTO formDataListDTO , PaasFormDataEsListVO paasFormDataEsListVO) throws XbbException;

    /**
     * 阶段解释不在返回子表单内部字段列表
     * @param explainList
     */
    protected void removeSubFormItems(List<FieldAttrEntity> explainList, Integer businessType) {
        Iterator<FieldAttrEntity> iterator = explainList.iterator();
        while (iterator.hasNext()) {
            FieldAttrEntity fieldAttrEntity = iterator.next();
            // 设置标签
            setTag(businessType, fieldAttrEntity);
            if (Objects.equals(FieldTypeEnum.SUB_FORM.getType(), fieldAttrEntity.getFieldType())) {
                // 把这里的子表单表头去掉，无需返回，在此不需要更改
                fieldAttrEntity.getSubForm().setItems(new ArrayList<>());
            }
        }
        // 客户电话设置不可见，拨号列应隐藏。
        if (Objects.equals(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(), businessType)) {
            Map<String, FieldAttrEntity> explainMap = new HashMap<>(explainList.size());
            explainList.forEach((item) -> explainMap.put(item.getAttr(), item));
            FieldAttrEntity fieldAttrEntity = explainMap.get(CustomerManagementEnum.PHONE.getAttr());
            if (fieldAttrEntity == null) {
                explainList.removeIf(fieldAttr -> Objects.equals(fieldAttr.getAttr(), ListOptionEnum.CALL.getAttr()));
            }
        } else if (Objects.equals(XbbRefTypeEnum.CLUE.getCode(), businessType)) {
            Map<String, FieldAttrEntity> explainMap = new HashMap<>(explainList.size());
            explainList.forEach((item) -> explainMap.put(item.getAttr(), item));
            FieldAttrEntity fieldAttrEntity = explainMap.get(ClueEnum.PHONE.getAttr());
            if (fieldAttrEntity == null) {
                explainList.removeIf(fieldAttr -> Objects.equals(fieldAttr.getAttr(), ListOptionEnum.CALL.getAttr()));
            }
        }
    }

    /**
     * 设置标签
     * @param businessType
     * @param fieldAttrEntity
     */
    private void setTag(Integer businessType, FieldAttrEntity fieldAttrEntity) {
        if (Objects.equals(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(), businessType)) {
            if (Objects.equals(fieldAttrEntity.getAttr(), CustomerManagementEnum.NAME.getAttr())) {
                fieldAttrEntity.setTagAttr(CustomerManagementEnum.NAME.getAttr() + "Tag");
            }
        } else if (Objects.equals(XbbRefTypeEnum.CONTRACT.getCode(), businessType)) {
            if (Objects.equals(fieldAttrEntity.getAttr(), ContractEnum.CONTRACT_NO.getAttr())) {
                fieldAttrEntity.setTagAttr(ContractEnum.CONTRACT_NO.getAttr() + "Tag");
            }
        } else if (Objects.equals(XbbRefTypeEnum.ORDER.getCode(), businessType)) {
            if (Objects.equals(fieldAttrEntity.getAttr(), OrderEnum.ORDER_NO.getAttr())) {
                fieldAttrEntity.setTagAttr(OrderEnum.ORDER_NO.getAttr() + "Tag");
            }
        } else if (Objects.equals(XbbRefTypeEnum.CLUE.getCode(), businessType)) {
            if (Objects.equals(fieldAttrEntity.getAttr(), ClueEnum.COMPANY_NAME.getAttr())) {
                fieldAttrEntity.setTagAttr(ClueEnum.COMPANY_NAME.getAttr() + "Tag");
            }
        }
    }

    protected void setOptions(FormDataListDTO formDataListDTO, PaasFormDataEsListVO paasFormDataEsListVO, ListOptionPackagePojo listOptionPackagePojo) throws XbbException {
        List<PaasFormDataEntityExt> dataList = paasFormDataEsListVO.getPaasFormDataESList();
        Map<String, FieldAttrEntity> explainMap = new HashMap<>(paasFormDataEsListVO.getHeadList().size());
        paasFormDataEsListVO.getHeadList().forEach(item->{
            explainMap.put(item.getAttr(), item);
        });
        for (PaasFormDataEntityExt item : dataList) {
            // 没有关联数据时，这个data是个null
            JSONObject data = item.getData() == null ? new JSONObject() : item.getData();
            item.setData(data);
            saasListHelp.setOptionValue(item, formDataListDTO, listOptionPackagePojo, formDataListDTO.getBusinessType(),
                    formDataListDTO.getSubBusinessType(), formDataListDTO.getListGroupId(), explainMap, new RelativeProductExplainVO());
        }

    }

}
